package com.h2n.framework.dao.classes;

import com.h2n.framework.dao.INewsDAO;
import com.h2n.framework.dto.NewsDTO;
import com.h2n.utils.EColor;
import com.h2n.utils.Convert;
import com.h2n.utils.EDatetimeFormat;

import java.util.*;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;


/**
 * Created by IntelliJ IDEA.
 * User: NhatNV
 * Date: Sep 11, 2009
 * Time: 11:36:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class NewsDAO implements INewsDAO {
  public NewsDAO(){
  }
  
  private JdbcTemplate jdbcTemplate;
  public JdbcTemplate getJdbcTemplate(){
    return jdbcTemplate;
  }
  public void setJdbcTemplate(JdbcTemplate _jdbcTemplate){
    jdbcTemplate = _jdbcTemplate;
  }

  @Override
  public long insert(NewsDTO _newsDTO) throws Exception {
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName(NewsDTO.INSERT_NAME);
    MapSqlParameterSource inParams = new MapSqlParameterSource(_newsDTO.getMap());
    Map outParams = simpleJdbcCall.execute(inParams);

    return Long.parseLong(outParams.get(NewsDTO.TableNews.p_news_item).toString());
  }

  @Override
  public void update(NewsDTO _newsDTO) throws Exception {
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName(NewsDTO.UPDATE_NAME);
    MapSqlParameterSource inParams = new MapSqlParameterSource(_newsDTO.getMap());
    simpleJdbcCall.execute(inParams);
  }

  @Override
  public void delete(int _newsItem) throws Exception {
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName(NewsDTO.DELETE_NAME);
    MapSqlParameterSource inParams = new MapSqlParameterSource(NewsDTO.TableNews.p_news_item.toString(),_newsItem);
    simpleJdbcCall.execute(inParams);
  }

  @Override
  public NewsDTO getInfo(int _newsItem) throws Exception {
    NewsDTO obj = null;
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName(NewsDTO.GET_INFO);
    MapSqlParameterSource inParams = new MapSqlParameterSource(NewsDTO.TableNews.p_news_item.toString(),_newsItem);
    Map outParams = simpleJdbcCall.execute(inParams);
    if(outParams != null){
      obj = new NewsDTO();
      obj.setContent(outParams.get(NewsDTO.TableNews.p_content.toString()).toString());
      obj.setContent_Color(outParams.get(NewsDTO.TableNews.p_content_color.toString()).toString());
      obj.setDate_Begin(Convert.stringToDate(outParams.get(NewsDTO.TableNews.p_date_begin.toString()).toString(), EDatetimeFormat.ddmmyyyyhhMMss));
      obj.setDate_End(Convert.stringToDate(outParams.get(NewsDTO.TableNews.p_date_end.toString()).toString(),EDatetimeFormat.ddmmyyyyhhMMss));
      obj.setHeader_Color(outParams.get(NewsDTO.TableNews.p_header_color.toString()).toString());
      obj.setIs_Locked(Integer.parseInt(outParams.get(NewsDTO.TableNews.p_is_locked.toString()).toString()));
      obj.setTitle(outParams.get(NewsDTO.TableNews.p_title.toString()).toString());
      obj.setNews_Item(Integer.parseInt(outParams.get(NewsDTO.TableNews.p_news_item.toString()).toString()));
    }

    return obj;
  }

  @Override
  public List<NewsDTO> getAll() throws Exception {
    jdbcTemplate.setResultsMapCaseInsensitive(true);
    SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName(NewsDTO.GET_ALL).returningResultSet(NewsDTO.TABLE_NAME, ParameterizedBeanPropertyRowMapper.newInstance(NewsDTO.class));
    Map outMap = simpleJdbcCall.execute(new HashMap<String,Object>(0));

    return (List)outMap.get(NewsDTO.TABLE_NAME);
  }
}
